import { Component, OnInit } from '@angular/core';
import { JsonApiService } from "app/core/api/json-api.service"; //http请求配置引入
import {config} from '../../shared/smartadmin.config';

declare var $: any;
@Component({
   selector: 'sa-transport-total',
   templateUrl: './transport-total.component.html',
})
export class TransportTotalComponent implements OnInit {
	tableData:any[] = []; //table数据
	totalCount:number = 0; //分页总条数
	curPage:number = 1; //分页当前页
	pageSize:number = 10; //内置的分页每页条数
	companyId:string = '';
	startTime:string = '';//开始时间
	endTime:string = '';//结束时间
	timeType:string = '0';//统计时间模式
	constructor(private jsonApiService: JsonApiService) {}

	ngOnInit() {
		this.initDate().then(()=>this.load());
	}
	//刷新
	refresh(){
		this.load();
	}
	//初始化时间
	initDate(){
		var that = this;
		return new Promise(function (resolve, reject) {
			System.import('assets/js/datepicker/js/bootstrap-datetimepicker.min.js').then(
				()=>{
					System.import('assets/js/datepicker/locales/bootstrap-datetimepicker.zh-CN.js').then(()=>{
						$("#datetimepicker1").datetimepicker({//时间插件
							format: "yyyy-mm",
						    language: "zh-CN",
						    autoclose: true,
						    startView: 3,
				            minView: 3,
						})
						.on("changeDate", function(e){
							var date = e.target.value;
							that.startTime = date;
//							$("#datetimepicker2").datetimepicker("setStartDate", date);
						});
						$("#datetimepicker2").datetimepicker({//时间插件
							format: "yyyy-mm",
						    language: "zh-CN",
						    autoclose: true,
						    startView: 3,
				            minView: 3,
						})
						.on("changeDate", function(e){
							var date = e.target.value;
							that.endTime = date;
//							$("#datetimepicker1").datetimepicker("setEndDate", date);
						});
						resolve();
					});
				}
			);
		})
	}
	timeTypeChange(timeType){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker3").val('').change();
		$("#datetimepicker4").val('').change();
		var that = this;
		let view = 3,format = 'yyyy-mm',datePicker1 = "#datetimepicker1",datePicker2 = "#datetimepicker2";
		if(timeType == '2'){
			view = 4;
			format = 'yyyy';
			datePicker1 = "#datetimepicker3";
			datePicker2 = "#datetimepicker4";
		}
		$(datePicker1).datetimepicker({//时间插件
			format: format,
		    language: "zh-CN",
		    autoclose: true,
		    startView: view,
            minView: view,
		})
		.on("changeDate", function(e){
			var date = e.target.value;
			that.startTime = date;
			$(datePicker2).datetimepicker("setStartDate", date);
		});
		$(datePicker2).datetimepicker({//时间插件
			format: format,
		    language: "zh-CN",
		    autoclose: true,
		    startView: view,
            minView: view,
		})
		.on("changeDate", function(e){
			var date = e.target.value;
			that.endTime = date;
			$(datePicker1).datetimepicker("setEndDate", date);
		});
	}
	//查询
	search_data(){
		this.curPage = 1;
		this.load();
	}
	//初始化加载list数据
	load() {
		if(this.timeType === '1'||this.timeType === '2'){
			if(!this.startTime||!this.endTime){
				this.jsonApiService.message('请选择时间范围');
				return;
			}
		}
		let dataobj = {
				"pageSize":this.pageSize,
				"nowPage":this.curPage,
				"parameters":{
						"startTime":this.startTime,
						"endTime":this.endTime,
						"companyId":this.companyId,
						'type':this.timeType
					},
			}
		let reqdata='gridPager='+JSON.stringify(dataobj);
		this.jsonApiService.http("stats/countTransportForPage.html", reqdata).subscribe(data => {
			if(data&&data.exhibitDatas){
				for(let item of data.exhibitDatas){
					item.totalTransWeightT = common.numberFormat(item.totalTransWeight);
					item.totalBridgeWeightT = common.numberFormat(item.totalBridgeWeight);
					item.totalTransDistanceT = common.numberFormat(item.totalTransDistance);
					item.totalBridgeDistanceT = common.numberFormat(item.totalBridgeDistance);
					item.totalTransHourT = common.twoDecimal(item.totalTransHour);
					item.totalBridgeHourT = common.twoDecimal(item.totalBridgeHour);
				}
			}
			this.tableData = data.exhibitDatas;
			this.totalCount = data.recordCount;
		})
	}
	//删除所选时间
	del_date(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker1").val('').change();
		$("#datetimepicker2").val('').change();
		$("#datetimepicker2").datetimepicker("setStartDate", null);
		$("#datetimepicker1").datetimepicker("setEndDate", null);
	}
	//删除所选时间
	del_date2(){
		this.startTime = '';
		this.endTime ='';
		$("#datetimepicker3").val('').change();
		$("#datetimepicker4").val('').change();
		$("#datetimepicker4").datetimepicker("setStartDate", null);
		$("#datetimepicker3").datetimepicker("setEndDate", null);
	}
	//分页回调
	paginate(e){
		this.curPage = e.page+1;
		this.pageSize = e.rows;
		this.load();
	}
	//公司选中回调
	companySelected(data){
		this.companyId = data.companyId;
	}
}
